<template>
  <div class="container">
    <div class="select-container">
      <el-input size="small" style="width: 200px;border:#eeeeee" class="filter-item" placeholder="代码名称" clearable
        @change="title_change" v-model="codeName" prefix-icon="el-icon-search">
      </el-input>
      <el-input size="small" style="width: 200px;" class="filter-item" placeholder="类别名称" clearable @change="entitle_change"
        v-model="codeTypeName" prefix-icon="el-icon-search">
      </el-input>
    </div>
    <div>
      <div class="filter">
        <el-row :gutter="20">
          <el-col :span="10">
            <span class="span-title">码表管理</span>
          </el-col>
          <el-col :span="14" style="text-align: right;">
            <el-button size="small" type="primary" @click="createClassifyClick" style="margin-right: 10px">新建分类</el-button>
            <el-button size="small" type="primary" @click="createClick" style="margin-right: 10px">新建码表</el-button>
          </el-col>
        </el-row>
      </div>
      <div class="data-contant">
        <div>
          <el-table :key='tableKey' :data="list.records" height="310" v-loading="listLoading" element-loading-text="给我一点时间"
            size="small" stripe style="width: 100%;border:#eeeeee" highlight-current-row>
            <el-table-column align="center" type="index" width="80" label="序号">
              <template slot-scope="scope">
              <span>{{scope.$index + (serialNumber - 1) * postList.rowBounds.limit + 1}}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="代码名称" prop="codeName">
            </el-table-column>
            <el-table-column align="center" label="类别名称" prop="codeTypeName">
            </el-table-column>
            <el-table-column align="center" label="代码类别" prop="codeType">
            </el-table-column>
            <el-table-column align="center" label="代码值" prop="codeValue">
            </el-table-column>
            <el-table-column align="center" label="状态" :formatter="fmtLength" prop="status">
            </el-table-column>
            <!-- <el-table-column v-show="fcShow"
              align="center"
              label="最后修改人"
              prop="fcUser"
            > -->
            </el-table-column>
            <el-table-column align="center" label="创建时间" :formatter="datetounix" prop="fcTime">
            </el-table-column>
            <el-table-column align="center" label="操 作" width="220px" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button style="width:24px; margin-left:-36px;" type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
                <el-button style="width:24px;" type="text" size="small" @click="handleDeleteState(scope.row)">删除</el-button>
                <el-button style="width:24px;" type="text" size="small" @click="handleDelete(scope.row)">删除数据</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination layout=" total, sizes, prev, pager, next"
             :page-size="this.postList.rowBounds.limit"
              :total="this.list.count"
              :page-sizes="[10,20,30, 50]"
              @current-change="current_change"
              @size-change="handleSizeChange">
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
    <!--新建分类弹出层-->
    <el-dialog
      v-if="dialogVisibleSysCode"
      :close-on-click-modal="false"
      title="新建分类"
      :visible.sync="dialogVisibleSysCode"
      width="70%">
      <CodeClassify @changeCloseDialog="changeCloseDialog"></CodeClassify>
    </el-dialog>
    <!--新建码表弹出层-->
    <el-dialog
      v-if="dialogVisibleNewSysCode"
      :close-on-click-modal="false"
      title="新建码表"
      :visible.sync="dialogVisibleNewSysCode"
      width="70%">
      <CodeAdd @changeCloseDialog="changeCloseDialog"></CodeAdd>
    </el-dialog>
    <!--编辑码表弹出层-->
    <el-dialog
      v-if="dialogVisibleEditSysCode"
      :close-on-click-modal="false"
      title="编辑码表"
      :visible.sync="dialogVisibleEditSysCode"
      width="70%">
      <CodeUpdate :listId="listId" @changeCloseDialog="changeCloseDialog"></CodeUpdate>
    </el-dialog>
  </div>
</template>

<script>
  import '@/styles/newstyle.scss'
  import CodeClassify from './components/codeClassify'
  import CodeUpdate from './components/codeUpdate'
  import CodeAdd from './components/codeAdd'
  const nameCondition = {
    'property': 'codeName',
    'value': '',
    'type': 'LIKE'
  }
  const ennameCondition = {
    'property': 'codeTypeName',
    'value': '',
    'type': 'LIKE'
  }
  const statusCondition = {
    property: 'status',
    value: '2',
    type: 'NE'
  }
  export default {
    data() {
      const checkTitle = (rule, value, callback) => {
        if (!value) {
          callback(new Error('子系统名称不能为空！'))
        } else if (value.length > 128) {
          callback(new Error('字符长度过长'))
        } else {
          callback()
        }
      }
      return {
        fcShow: false,
        dialogVisibleSysCode: false,
        dialogVisibleNewSysCode: false,
        dialogVisibleEditSysCode: false,
        codeName:'',
        codeTypeName:'',
        listId: {},
        tableKey: 0,
        list: {},
        postList: {
          condition: [{
            property: 'status',
            value: '2',
            type: 'NE'
          }],
          sort: [{
            direction: 'DESC',
            property: 'fcTime'
          }],
          rowBounds: {
            offset: 0,
            limit: 10
          }
        },
        listLoading: false,
        fliter: [
          Object.assign({}, nameCondition),
          Object.assign({}, ennameCondition),
          Object.assign({}, statusCondition)
        ],
        updatelist: {},
        updaterules: {
          name: [{
            validator: checkTitle,
            required: true,
            trigger: 'blur'
          }],
          nameEn: [{
            message: '子系统名称英文',
            required: true,
            trigger: 'blur'
          },
          {
            min: 1,
            max: 128,
            message: '长度在 1 到 128 个字符',
            trigger: 'blur'
          }
          ],
          url: [{
            message: '子系统访问URL',
            required: true,
            trigger: 'blur'
          },
          {
            min: 1,
            max: 256,
            message: '长度在 1 到 256 个字符',
            trigger: 'blur'
          }
          ],
          code: [{
            message: '子系统访问URL',
            required: true,
            trigger: 'blur'
          },
          {
            min: 1,
            max: 32,
            message: '长度在 1 到 32 个字符',
            trigger: 'blur'
          }
          ]
        },
        deletelist: {},
        deleteStatelist: {},
        serialNumber:1
      }
    },
    components: {
      CodeClassify,
      CodeUpdate,
      CodeAdd
    },
    // 画面刚进入method
    created() {
      this.fetchData()
    },
    methods: {
      changeCloseDialog(val) {
        this.dialogVisibleSysCode = false
        this.dialogVisibleNewSysCode = false
        this.dialogVisibleEditSysCode = false
        this.fetchData()
      },
      datetounix(row, column) {
        var date = row[column.property]
        if (date == undefined) {
          return ''
        }
        var demo01 = date.split('')
        var demo02 = demo01.slice(0, 4).join('')
        var demo03 = demo01.slice(4, 6).join('')
        var demo04 = demo01.slice(6, 8).join('')
        var demo05 = demo01.slice(8, 10).join('')
        var demo06 = demo01.slice(10, 12).join('')
        var demo07 = demo01.slice(12).join('')
        var demo = demo02 + '-' + demo03 + '-' + demo04 + ' ' + demo05 + ':' + demo06 + ':' + demo07
        return demo
      },
      fetchData() {
        this.listLoading = true
        this.$store.dispatch('CodeSelect', this.postList).then(res => {
          this.list = res.data
          this.listLoading = false
        }).catch((err) => {
          console.log(err)
        })
      },
      updateData() {
        this.$store.dispatch('CodUpdate', this.updatelist).then(res => {
          this.fetchData()
          this.$refs['updatelist'].resetFields()
        }).catch((err) => {
          console.log(err)
        })
      },
      deleteStateData() {
        this.$store.dispatch('CodeDeleteState', this.deleteStatelist).then(res => {
          this.fetchData()
        }).catch((err) => {
          console.log(err)
        })
      },
      deleteData() {
        this.$store.dispatch('CodeDelete', this.deletelist).then(res => {
          this.fetchData()
        }).catch((err) => {
          console.log(err)
        })
      },
      handleDeleteState(row) {
        this.$confirm('是否删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteStatelist.codeType = row.codeType
          this.deleteStatelist.codeValue = row.codeValue
          this.deleteStateData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      handleDelete(row) {
        this.$confirm('是否强制删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deletelist.codeType = row.codeType
          this.deletelist.codeValue = row.codeValue
          this.deleteData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      createClick() {
        this.dialogVisibleNewSysCode = true
      },
      createClassifyClick() {
        this.dialogVisibleSysCode = true
      },
      handleEdit(row) {
        this.listId = row
        this.dialogVisibleEditSysCode = true
      },
      fmtLength(row, column) {
        const arr = row[column.property]
        /* 这里的 if(arr === undefined)
         * 是根据自己的需求，决定arr长度为0时的判断条件
         * 可以和这个不同
         */
        if (arr === '0') {
          return '未禁用'
        } else if (arr === '1') {
          return '已禁用'
        }
      },
      current_change(currentPage) { // 通过分页获取数据
        this.serialNumber =currentPage
        const page = currentPage - 1
        this.postList.rowBounds.offset = page * 10
        this.fetchData()
      },
      // 翻页器使用
      handleSizeChange(val) {
        this.postList.rowBounds.limit = val
        this.postList.rowBounds.offset = this.postList.rowBounds.limit * (this.serialNumber - 1)
        this.fetchData()
      },
      title_change(type) { // 分类数据
        this.serialNumber = 1
        if (type === null) {
          const nullfliter = this.fliter.concat()
          const condition = []
          for (let i = 0; i < nullfliter.length; i++) {
            if (nullfliter[i].property === 'codeName') {
              nullfliter.splice(i, 1)
              this.fliter[i].value = ''
            }
          }
          for (let i = 0; i < nullfliter.length; i++) {
            if (nullfliter[i].value === '') {
              continue
            }
            condition.push(nullfliter[i])
          }
          this.postList.condition = condition
          this.postList.rowBounds = {
            'offset': 0,
            'limit': 10
          }
          this.fetchData()
        } else {
          const conditionTimefliter = this.fliter.concat()
          const condition = []
          for (let i = 0; i < conditionTimefliter.length; i++) {
            if (conditionTimefliter[i].property === 'codeName') {
              conditionTimefliter[i].value = type
              this.fliter[i].value = type
            }
          }
          for (let i = 0; i < conditionTimefliter.length; i++) {
            if (conditionTimefliter[i].value === '') {
              continue
            }
            condition.push(conditionTimefliter[i])
          }
          this.postList.condition = condition
          this.postList.rowBounds = {
            'offset': 0,
            'limit': 10
          }
          this.fetchData()
        }
      },
      entitle_change(type) { // 分类数据
        this.serialNumber = 1
        if (type === null) {
          const nullfliter = this.fliter.concat()
          const condition = []
          for (let i = 0; i < nullfliter.length; i++) {
            if (nullfliter[i].property === 'codeTypeName') {
              nullfliter.splice(i, 1)
              this.fliter[i].value = ''
            }
          }
          for (let i = 0; i < nullfliter.length; i++) {
            if (nullfliter[i].value === '') {
              continue
            }
            condition.push(nullfliter[i])
          }
          this.postList.condition = condition
          this.postList.rowBounds = {
            'offset': 0,
            'limit': 10
          }
          this.fetchData()
        } else {
          const conditionTimefliter = this.fliter.concat()
          const condition = []
          for (let i = 0; i < conditionTimefliter.length; i++) {
            if (conditionTimefliter[i].property === 'codeTypeName') {
              conditionTimefliter[i].value = type
              this.fliter[i].value = type
            }
          }
          for (let i = 0; i < conditionTimefliter.length; i++) {
            if (conditionTimefliter[i].value === '') {
              continue
            }
            condition.push(conditionTimefliter[i])
          }
          this.postList.condition = condition
          this.postList.rowBounds = {
            'offset': 0,
            'limit': 10
          }
          this.fetchData()
        }
      }
    }
  }

</script>

<style scoped>
  .container {
    padding: 1%;
    height: 100%;
    width: 100%;
    overflow-y: auto;
  }

  .select-container {
    padding: 10px 10px;
    margin-bottom: 10px;
    border-radius: 4px;
    border: 1px solid#e4e7ed;
  }

  .list-container {
    margin-top: 30px;
    padding: 0 10px 10px 10px;
    border: 1px solid#e4e7ed;
    border-radius: 4px;
  }

  .data-contant {
    padding: 30px 10px 20px;
  }

  .data-detail {
    font-size: 14px;
    padding: 20px 0;
    text-align: center;
  }

  .data-title {
    font-size: 14px;
    padding: 10px 0;
    text-align: center;
  }

  .data-title div {
    font-size: 14px;
    padding: 5px 0;
  }

  .filter {
    padding: 10px 5px;
    border-bottom: 2px solid#e4e7ed;
  }

  .list-container {}

  .bold {
    font-weight: bold;
  }

</style>
